package com.jy.wms.service;

import com.jy.wms.common.annotions.BaseServiceDescribe;
import com.jy.wms.common.base.BaseService;
import com.jy.wms.common.base.ServiceException;
import com.jy.wms.common.base.SucessException;
import com.jy.wms.dao.*;
import com.jy.wms.pojo.*;
import com.xiaoleilu.hutool.date.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @ClassName: WmsQualityInspectionListService.java
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @Author Jane
 * @Date Mon Nov 06 14:46:06 CST 2017
 */

@Service
public class WmsQualityInspectionListService extends BaseService<WmsQualityInspectionListMapper,WmsQualityInspectionList> {
    @Autowired
    private WmsQualityTaskPdaMapper wmsQualityTaskPdaMapper;
    @Autowired
    private WmsQualityTaskDetailedPdaMapper wmsQualityTaskDetailedPdaMapper;
    @Autowired
    private WmsPdaMsgMapper wmsPdaMsgMapper;
    @Autowired
    private WmsQualityInspectionListDetailedMapper wmsQualityInspectionListDetailedMapper;
    /**
     * 插入一条记录
     * @param entity 实体对象
     * @return int
     */
    public Integer baseInsert( WmsQualityInspectionList entity){
        return dao.baseInsert( entity );
    }

    /**
     * 插入一条记录（匹配有值的字段）
     * @param entity 实体对象
     * @return int
     */
    public Integer baseInsertSelective( WmsQualityInspectionList entity){
        return dao.baseInsertSelective( entity);
    }

    /**
     * 删除（根据主键ID删除）
     * @param id 主键
     * @return int
     */
    public Integer baseDeleteByPrimaryKey( String id){
        return dao.baseDeleteByPrimaryKey( id);
    }

    /**
     * 删除（根据ID 批量删除）
     * @param idList 主键ID列表
     * @return int
     */
    public Integer baseDeleteBathPrimaryKeys( List<String> idList){
        return dao.baseDeleteBathPrimaryKeys( idList );
    }

    /**
     * 分页查询_Jane
     * @param entity 实体
     * @return Page<WmsQualityInspectionList>
     */
    @BaseServiceDescribe(moduleName = "查询质检信息",btnName = "查询质检信息")
    public Page<WmsQualityInspectionList> baseSelectPage(WmsQualityInspectionList entity ){
        PageHelper.startPage(entity.getPage(),entity.getRows());
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if(entity.getCjsj_start()!=null){
            Date date =new Date(new Long(entity.getCjsj_start()));
            entity.setCjsj_start(format.format(date));
        }
        if(entity.getCjsj_end()!=null){
            Date date =new Date(new Long(entity.getCjsj_end()));
            entity.setCjsj_end(format.format(date));
        }
        return (Page<WmsQualityInspectionList>)dao.baseSelectPage( entity );
    }
    /**
     * 查询质检出库任务信息_Jane
     * @param entity 实体
     * @return Page<WmsQualityInspectionList>
     */
    @BaseServiceDescribe(moduleName = "查询质检出库任务信息",btnName = "查询质检出库任务信息")
    public Page<WmsQualityInspectionList> baseCkRwSelectPage(WmsQualityInspectionList entity ){
        PageHelper.startPage(entity.getPage(),entity.getRows());
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if(entity.getCjsj_start()!=null){
            Date date =new Date(new Long(entity.getCjsj_start()));
            entity.setCjsj_start(format.format(date));
        }
        if(entity.getCjsj_end()!=null){
            Date date =new Date(new Long(entity.getCjsj_end()));
            entity.setCjsj_end(format.format(date));
        }
        return (Page<WmsQualityInspectionList>)dao.baseCkRwSelectPage(entity);
    }
    /**
     * 查询质检入库任务信息_Jane
     * @param entity 实体
     * @return Page<WmsQualityInspectionList>
     */
    @BaseServiceDescribe(moduleName = "查询质检入库任务信息",btnName = "查询质检入库任务信息")
    public Page<WmsQualityInspectionList> baseRkrwSelectPage(WmsQualityInspectionList entity ){
        PageHelper.startPage(entity.getPage(),entity.getRows());
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if(entity.getCjsj_start()!=null){
            Date date =new Date(new Long(entity.getCjsj_start()));
            entity.setCjsj_start(format.format(date));
        }
        if(entity.getCjsj_end()!=null){
            Date date =new Date(new Long(entity.getCjsj_end()));
            entity.setCjsj_end(format.format(date));
        }
        return (Page<WmsQualityInspectionList>)dao.baseRkrwSelectPage(entity);
    }
    /**
     * 查询质检入库任务信息_Jane
     * @param hzid 实体
     * @return Page<WmsQualityInspectionList>
     */
    @BaseServiceDescribe(moduleName = "质检出入库任务",btnName = "查询PDA登录用户")
    public List<Map<String,Object>> baseSelectPDADLByHz(String hzid,String ckid,String dlyhmc ){
        List<Map<String,Object>> map=new ArrayList<>();
        List<WmsPdaLogon> logon=dao.baseSelectPDADLByHz(hzid,ckid,dlyhmc);
        for(WmsPdaLogon logon1:logon){
            Map<String,Object> objectMap=new HashMap<>();
            objectMap.put("id",logon1.getDlyhid());
            objectMap.put("name",logon1.getDlyhmc());
            map.add(objectMap);
        }
        return map;
    }
    /**
     * 生成质检任务_Jane
     * @param zjdid 实体
     * @return
     */
    @BaseServiceDescribe(moduleName = "质检出库任务",btnName = "生成质检任务")
    @Transactional
    public void addZjckrw(String zjdid,String ryid)throws ServiceException,SucessException{
        WmsQualityInspectionList list=dao.baseSelectByPrimaryKey(zjdid);
        if(list!=null){
            if(list.getCkrwzt()==0){
                //生成PDA质检任务
                WmsQualityTaskPda task=new WmsQualityTaskPda();
                task.setCkid(list.getCkid());
                task.setAsndh(list.getAsnbh());
                task.setAsnid(list.getAsnid());
                task.setRyid(','+ryid+',');
                task.setSshzid(list.getSshzid());
                task.setZjdh(list.getZjdh());
                task.setZjdid(list.getId());
                WmsEstimatedArrivalOrder order=dao.seachMpztByAsnId(list.getAsnid());
                if("1".equals(order.getMpzt())){
                    task.setShlx("1");
                }else{
                    task.setShlx("0");
                }
                int taskStatus= wmsQualityTaskPdaMapper.baseInsert(task);
                if(taskStatus>0){
                    //生成PDA质检任务明细
                    List<WmsQualityTaskDetailedPda> pdad= wmsQualityTaskDetailedPdaMapper.seachDetailedByZjdid(zjdid,order.getMpzt());
                    for(WmsQualityTaskDetailedPda d:pdad){
                        d.setQpdaid(task.getId().toString());
                        if(d.getLpn()==null){d.setLpn("");}
                        wmsQualityTaskDetailedPdaMapper.baseInsert(d);
                    }
                }else {
                    throw new RuntimeException("生成PDA质检任务失败");
                }
                SimpleDateFormat format=new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
                //给PDA推送消息
                WmsPdaMsg msg = new WmsPdaMsg();
                msg.setRyid(ryid);
                msg.setBt(list.getAsnbh());
                msg.setXx(format.format(new Date())+"生成质检单出库任务");
                wmsPdaMsgMapper.baseInsert(msg);
                //改变质检单任务状态
                WmsQualityInspectionList quality=new WmsQualityInspectionList();
                quality.setId(zjdid);
                quality.setCkrwfpsj(format.format(new Date()));
                quality.setCkrwzt(1);
                quality.setCkrwryid(ryid);
                dao.baseUpdateByPrimaryKeySelective(quality);
            }else{
                throw new ServiceException("此质检单不能生成质检任务");
            }
        }else{
            throw new ServiceException("没有此质检单号");
        }
    }
    /**
     * 生成质检任务_Jane
     * @param zjdid 实体
     * @return
     */
    @BaseServiceDescribe(moduleName = "质检入库任务",btnName = "生成入库任务")
    @Transactional
    public void addZjrkrw(String zjdid,String ryid)throws ServiceException,SucessException{
        WmsQualityInspectionList list=dao.baseSelectByPrimaryKey(zjdid);
        if(list!=null){
            if(list.getRkrwzt()==0&&"2".equals(list.getZt())){
                //生成PDA质检任务
                WmsQualityTaskPda t=wmsQualityTaskPdaMapper.baseSelectByzjdid(zjdid);
                WmsQualityTaskPda task=new WmsQualityTaskPda();
                task.setId(t.getId());
                task.setRyid(','+ryid+',');
                task.setZjzt("2");
                int taskStatus= wmsQualityTaskPdaMapper.baseUpdateByPrimaryKeySelective(task);
                if(taskStatus>0){
                    //生成PDA质检任务明细
                    List<WmsQualityInspectionListDetailed> ins= wmsQualityInspectionListDetailedMapper.baseSelectByZjdid(zjdid);
                    WmsQualityTaskDetailedPda de=null;
                    for(WmsQualityInspectionListDetailed d:ins){
                        de=new WmsQualityTaskDetailedPda();
                        if(Integer.parseInt(d.getYjsl())-d.getYpsl()==0){
                            continue;
                        }else if (Integer.parseInt(d.getYjsl())-d.getYpsl()>0){
                            de.setYjsl(String.valueOf(Integer.parseInt(d.getYjsl())-d.getYpsl()));
                        }
                        de.setZt("0");
                        de.setZjdmxid(d.getId());

                        wmsQualityTaskDetailedPdaMapper.baseUpdateByZjdmxidSelective(de);
                    }
                }
                SimpleDateFormat format=new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
                //给PDA推送消息
                WmsPdaMsg msg = new WmsPdaMsg();
                msg.setRyid(ryid);
                msg.setBt(list.getAsnbh());
                msg.setXx(format.format(new Date())+"生成质检单入库任务");
                wmsPdaMsgMapper.baseInsert(msg);
                //改变质检单任务状态
                WmsQualityInspectionList quality=new WmsQualityInspectionList();
                quality.setId(zjdid);
                quality.setRkrwfpsj(format.format(new Date()));
                quality.setRkrwzt(1);
                quality.setRkrwryid(ryid);
                dao.baseUpdateByPrimaryKeySelective(quality);

            }else{
                throw new ServiceException("此质检单不能生成质检任务");
            }
        }else{
            throw new ServiceException("没有此质检单号");
        }
    }
    /**
     * 查询（根据主键ID查询）
     * @param id 主键ID
     * @return WmsQualityInspectionList
     */
    public WmsQualityInspectionList baseSelectByPrimaryKey( String id ){
        return dao.baseSelectByPrimaryKey( id );
    }

    /**
     * 查询（根据ID 批量查询）
     * @param idList 主键ID列表
     * @return list
     */
    public Integer baseSelectBathPrimaryKeys( List<WmsQualityInspectionList> idList ){
        return dao.baseSelectBathPrimaryKeys( idList );
    }

    /**
     *修改 （匹配有值的字段）
     * @param entity 主键ID列表
     * @return int
     */
    public Integer baseUpdateByPrimaryKeySelective( WmsQualityInspectionList entity){
        return dao.baseUpdateByPrimaryKeySelective( entity);
    }

    /**
     *修改（根据主键ID修改）
     * @param entity 主键ID列表
     * @return int
     */
    public Integer baseUpdateByPrimaryKey( WmsQualityInspectionList entity){
        return dao.baseUpdateByPrimaryKey( entity);
    }
}
